/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dbclasses;

import classes.Card;
import classes.Customer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 * Class with methods for dealing insert of Customer and Card objects
 * @author keaneb1
 */
public class CustomerDB {
    private static Connection conn = null;
    private static PreparedStatement prest = null;
    
    /**
     * Method for inserting a new customer into the database.
     * @param newCustomer the Customer object to be stored in the database
     * @return the boolean of true if the insert was successful.
     */
    public static boolean insertCustomer(Customer newCustomer){
        
        //create the SQL statement for inserting a customer into the database
        String query = "INSERT INTO customer (firstname,lastname,email,licenseno,expirydate,creditcardno) VALUES (?,?,?,?,?,?)";
        boolean success = false;
        /*
         * Create a new connection, execute the insert statement and save the customer into the database.
         */
        conn = Dbconnector.getConnected();
        if(conn!=null){
            try {
                //execute the insert statement filling each column in the table
                prest = conn.prepareStatement(query);
                prest.setString(1, newCustomer.getFirstName());
                prest.setString(2, newCustomer.getLastName());
                prest.setString(3, newCustomer.getEmail());
                prest.setString(4, newCustomer.getLicenseNo());
                prest.setDate(5, newCustomer.getLicenseExpire());
                prest.setString(6, newCustomer.getCreditCard().getCardNumber());
                prest.executeUpdate();
                conn.close();
                success = true;
            
            } catch (SQLException ex) {
                //tell the client which method call errored out and what was the message
                JOptionPane.showMessageDialog(null, "Problem in insertCustomer " +ex.getSQLState());
            }
        }
        //the value of success if the insert succeeded or not
        return success;
    }
    
    /**
     * Method to insert a new credit card into the database.
     * @param newCard the new Card object to be stored in the database.
     * @return the boolean of true if the save was successful
     */
    public static boolean insertCard(Card newCard){
        
        //create the SQL statement for inserting a credit card into the database
        String query = "INSERT INTO creditcard (creditcardno,personname,type,cardExpiry,csv) VALUES (?,?,?,?,?)";
        boolean success = false;
        /*
         * Create a new connection, execute the insert statement to put the Card into the database.
         */
       conn = Dbconnector.getConnected();
        if(conn!=null){
            try {
                //execute the insert statement filling each column in the table
                prest = conn.prepareStatement(query);
                prest.setString(1, newCard.getCardNumber());
                prest.setString(2, newCard.getPersonName());
                prest.setString(3, newCard.getCardType());
                prest.setString(4, newCard.getExpirationDate());
                prest.setInt(5, newCard.getCsvNumber());
                prest.executeUpdate();
                conn.close();
                success = true;
            
            } catch (SQLException ex) {
                //tell the client which method call errored out and what was the message
                JOptionPane.showMessageDialog(null, "Problem in insertCard " +ex.getSQLState());
            }
        }
        //the value of success if the insert succeeded or not
        return success;
    }
}
